package com.mhys.ynnz_ssm.business.user.controller;

import com.mhys.ynnz_ssm.business.sys.annotations.log.MySysLog;
import com.mhys.ynnz_ssm.business.user.dto.MhysUserDO;
import com.mhys.ynnz_ssm.business.user.dto.MhysUserVO;
import com.mhys.ynnz_ssm.business.user.service.UserService;
import com.mhys.ynnz_ssm.core.dto.MyPager;
import com.mhys.ynnz_ssm.core.dto.MyResult;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户管理控制器
 * 2024/6/11
 */
@Controller
@RequestMapping("/user")
@Slf4j // 开启日志记录
public class UserController {

    @Resource(name="userServiceImpl")
    private UserService userService;

    /**
     * 查询用户的分页信息
     * @param userVO 查询条件
     * @param pager 分页条件
     * @return
     */
    @GetMapping("/getUserPagers") // http://localhost:9010/ynnz_ssm/user/getUserPagers?page=1&size=2&userName=55
    @ResponseBody
    public MyResult getUserPagers(MhysUserVO userVO, MyPager pager){
        log.info("--查询用户的分页信息:userVO："+userVO+";pager:"+pager);

        MyResult result = new MyResult();

        MyPager<MhysUserDO> pages = userService.getUserPagers(userVO,pager);

        result.setData(pages);

        return result;
    }

    /**
     * 根据用户id获取用户信息
     * @param userID 用户id
     * @return
     */
    @GetMapping("/getUserByID") // http://localhost:9010/ynnz_ssm/user/getUserByID?userID=101
    @ResponseBody
    public MyResult getUserByID(String userID){

        log.info("--根据用户id获取用户信息:userID:"+userID);

        MyResult result = new MyResult();

        MhysUserVO userVO = userService.getUserByID(userID);

        result.setData(userVO);

        return result;
    }

    /**
     * 添加用户信息
     * @param userVO
     * @return
     */
    @PostMapping("/addUser") // http://localhost:9010/ynnz_ssm/user/addUser
    @ResponseBody
    public MyResult addUser(@RequestBody MhysUserVO userVO){

        log.info("--添加用户信息:userVO:"+userVO);

        MyResult result = new MyResult();

        int len = userService.addUser(userVO);

        if(len>0){
            result.setMsg("用户添加成功！");
        }else if(len == -1){
            result.setMsg("用户名已存在，请更换用户名！");
            result.setCode(500);
        }else{
            result.setMsg("用户添加失败！");
            result.setCode(500);
        }

        return result;
    }




    /**
     * 修改用户信息
     * @param userVO
     * @return
     */
    @PutMapping("/updateUser") // http://localhost:9010/ynnz_ssm/user/updateUser
    @ResponseBody
    public MyResult updateUser(@RequestBody MhysUserVO userVO){

        log.info("--修改用户信息:userVO:"+userVO);

        MyResult result = new MyResult();

        int len = userService.updateUser(userVO);

        if(len>0){
            result.setMsg("用户修改成功！");
        }else{
            result.setMsg("用户修改成功！");
            result.setCode(500);
        }

        return result;
    }

    /**
     * 删除用户信息
     * @param userID
     *      单行删除传入一个id
     *      多行删除 将id用逗号分隔
     * @return
     */
    @DeleteMapping ("/deleteUser") // http://localhost:9010/ynnz_ssm/user/deleteUser?userID=101
    @ResponseBody
    public MyResult deleteUser(String userID){

        log.info("--删除用户信息:userID:"+userID);

        MyResult result = new MyResult();

        int len = userService.deleteUser(userID);

        if(len>0){
            result.setMsg("用户删除成功！");
        }else{
            result.setMsg("用户删除失败！");
            result.setCode(500);
        }

        return result;
    }

    /**
     * 获取验证码方法
     * @param request 请求对象
     * @param response 响应对象
     * @return
     */
    @GetMapping("/captcha")
    @ResponseBody
    public MyResult captcha(HttpServletRequest request, HttpServletResponse response) {

        MyResult result = new MyResult<>();

        // 创建GifCaptcha对象，设置宽度为160，高度为40，长度为5
        GifCaptcha gifCaptcha = new GifCaptcha(160, 40, 5);
        // 获取生成的验证码内容
        final String captcha = gifCaptcha.text().toLowerCase();
        System.out.println("---验证码："+captcha);

        //3. 将验证码数据存入session
        request.getSession().setAttribute("captcha", captcha);

        // 4.将验证码图片转换为base64格式
        String base64 = gifCaptcha.toBase64();

        result.setMsg("验证码获取成功！");
        result.setData(base64);

        return result;
    }

    /**
     * 登录操作
     * @param request 请求对象用于获取session
     * @param userVO 请求的登录表单数据
     * @return
     */
    @PostMapping("/doLogin")
    @ResponseBody
    // methodType: get:1 post:2 put:3 delete:4  其他:5
    @MySysLog(modelName = "登录功能",methodDesc = "进行登录",methodType = 2)
    public MyResult doLogin(HttpServletRequest request,@RequestBody MhysUserVO userVO){

        log.info("--登录操作:userVO:"+userVO);

        MyResult result = new MyResult();

        // 1.先验证验证码是否正确
        Object captcha = request.getSession().getAttribute("captcha");
        if(captcha == null){ // 验证码过期 状态码 501
            result.setMsg("验证码过期！");
            result.setCode(501);
            return result;
        }else{
            String serverCode = (String)captcha;
            if(serverCode.equalsIgnoreCase(userVO.getReCode())){ // 验证通过

                // 验证通过后调用service进行用户名和密码的判断
                result = userService.login(request,userVO);
                return result;
            }else{// 验证码验证不通过  验证码输入错误 状态码 500
                result.setMsg("验证码输入错误！");
                result.setCode(500);
                return result;
            }
        }
    }

}
